$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'ClassCourse2/Select',
        datatype: "json",
        colModel: [
            { label: '班级名称', name: 'className', width: 80,key: true },
             { label: '选课课号', name: 'courseSchoolId', width: 100 },
             { label: '上课地点', name: 'courseClassroomPosition', width: 60 },
             { label: '课程名称', name: 'courseName', width: 60 },
             { label: '学年', name: 'courseAcademicYear', width: 40 },
             { label: '学期', name: 'courseAcademicTerm', width: 20 },
             { label: '教学周', name: 'courseWhichWeek', width: 20 },
             { label: '星期几', name: 'courseWhichDay', width: 20 },
             { label: '第几节', name: 'courseWhichIndex', width: 20 },
             { label: '教师职工号', name: 'courseTeacherId', width: 40 },
             { label: '教师姓名', name: 'courseTeacherName', width: 40 }
         ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    new AjaxUpload('#upload', {
        action: baseURL + 'ClassCourse2/upload?token=' + token,
        name: 'file',
        autoSubmit:true,
        responseType:"json",
        onSubmit:function(file, extension){
            if (!(extension && /^(xls|xlsx)$/.test(extension.toLowerCase()))){
                alert('只支持excel上传！');
                return false;
            }
        },
        onComplete : function(file, r){
            if(r.code == 0){
                alert("上传成功");
            }else{
                alert(r.msg);
            }
        }
    });
    /*var websocket=null;
    if('WebSocket' in window){
         websocket=new WebSocket("ws://localhost:8081/iyx/webSocket?token="+token);//呜哇 nice
    } else{
        alert("浏览器不支持webSocket")
    }
    websocket.onerror = function(){
        alert('webSocket通信发生错误')
    };
    //连接成功建立的回调方法
    websocket.onopen = function(event){
        console.log('建立连接');
    }

    //接收到消息的回调方法
    websocket.onmessage = function(event){
        console.log('收到消息:'+event.data);
        //alert(event.data);
        //收到消息 推进进度条
         $CommonUI.getProgressBar('#importProgress').progressbar('setValue', event.data);
        vm.openFullScreen();
    }
    //连接关闭的回调方法
    websocket.onclose = function(){
         console.log('连接关闭');
    }
    //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
    window.onbeforeunload = function(){
            websocket.close();
    }
    //关闭连接
    function closeWebSocket(){
            websocket.close();
    }
    //发送消息
    function send(){
        var message = document.getElementById('text').value;
        websocket.send(message);
    }*/
});
var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            key: null
        },
        showList: true,
        title: null,
        ClassCourse2: {},
        className: null,
        uploadCount:null,
        dialogProgressVisible:false,
        webSocket:null
    },
    created(){
        this.initWebSocket()
    },
    methods: {
        initWebSocket:function(){
            this.webSocket=new WebSocket("ws://"+localStorage.getItem("webSocketPort")+"/webSocket?token="+token);
            this.webSocket.onmessage=this.websocketonmessage;
            this.webSocket.onclose = this.websocketclose;
            console.log("建立连接")
        },
        websocketonmessage:function(e){ //数据接收
            console.log(e.data);
            vm.uploadCount=e.data;
            vm.dialogProgressVisible=true;
            if(e.data=="100.0"){
             vm.dialogProgressVisible=false;
            }
        },
        websocketsend(agentData){//数据发送
            this.websock.send(agentData);
        },
        websocketclose(e){  //关闭
            console.log("connection closed (" + e.code + ")");
        },
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.ClassCourse2 = {};
        },
        update: function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            $.get(baseURL + "ClassCourse2/Info/"+id, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.ClassCourse2 = r.ClassCourse2;
                vm.className = r.ClassCourse2.className;
            });
        },
        upload: function () {

        },
        del: function () {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "ClassCourse2/Delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
         saveOrUpdate: function () {
            if(vm.validator()){
                return ;
            }

            var url = vm.ClassCourse2.className == vm.className ? "ClassCourse2/Update" : "ClassCourse2/Insert";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.ClassCourse2),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{ 'className': vm.q.key},
                page:page
            }).trigger("reloadGrid");
        },
        validator: function () {
            if(isBlank(vm.ClassCourse2.className)){
                alert("班级名称不能为空");
                return true;
            }
            if(isBlank(vm.ClassCourse2.courseSchoolId)){
                alert("选课课号不能为空");
                return true;
            }
            if(isBlank(vm.ClassCourse2.courseClassroomPosition)){
                alert("上课地点不能为空");
                return true;
            }
            if(isBlank(vm.ClassCourse2.courseName)){
                alert("课程名称不能为空");
                return true;
            }
            if(isBlank(vm.ClassCourse2.courseAcademicYear)){
                alert("学年不能为空");
                return true;
            }
            if(isBlank(vm.ClassCourse2.courseAcademicTerm)){
                alert("学期不能为空");
                return true;
            }
            if(isBlank(vm.ClassCourse2.courseWhichWeek)){
                alert("教学周不能为空");
                return true;
            }
            if(isBlank(vm.ClassCourse2.courseWhichDay)){
                alert("星期几不能为空");
                return true;
            }
            if(isBlank(vm.ClassCourse2.courseWhichIndex)){
                alert("第几节不能为空");
                return true;
            }
            if(isBlank(vm.ClassCourse2.courseTeacherId)){
                alert("教师职工号不能为空");
                return true;
            }
            if(isBlank(vm.ClassCourse2.courseTeacherName)){
                alert("教师姓名不能为空");
                return true;
            }
            }
        }
});